#include "ir.h"

/*
* Ir input pin
* mode:floating input
* pin: PA1
* GPIO_AF: TIM2_CH2
*/
void Ir_Pin_Init()
{
    GPIO_InitType GPIO_InitStructure;

    GPIO_InitStruct(&GPIO_InitStructure);
    /* TIM3 channel 2 pin (PA.07) configuration */
    GPIO_InitStructure.Pin        = GPIO_PIN_8;
    GPIO_InitStructure.GPIO_Mode  = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Current = GPIO_DC_LOW;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_TIM1;
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
}

void Remote_Init()
{
    NVIC_InitType NVIC_InitStructure;

		TIM_TimeBaseInitType         TIM_TimeBaseStructure;
    TIM_ICInitType               TIM_ICInitStructure;

		RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);

    Ir_Pin_Init();

//    TIM_ClearITPendingBit(TIM2,TIM_IT_Update|TIM_IT_CC2); 

    TIM_TimeBaseStructure.Period = 1000;
    TIM_TimeBaseStructure.Prescaler = 480-1; 
    TIM_TimeBaseStructure.ClkDiv = 0; 
		TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure); 

    TIM_ICInitStructure.Channel = TIM_CH_2; 
    TIM_ICInitStructure.IcPolarity = TIM_IC_POLARITY_RISING;
    TIM_ICInitStructure.IcSelection = TIM_IC_SELECTION_DIRECTTI;
    TIM_ICInitStructure.IcPrescaler = TIM_IC_PSC_DIV1;  
    TIM_ICInitStructure.IcFilter = 0x03;
    TIM_ICInit(TIM1, &TIM_ICInitStructure);

    /* Enable the TIM3 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority           = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Enable the CC2 Interrupt Request */
    TIM_ConfigInt(TIM1, TIM_INT_UPDATE|TIM_INT_CC2, ENABLE);
    /* TIM enable counter */
    TIM_Enable(TIM1, ENABLE);
}

uint8_t  RmtSta = 0;
uint16_t Dval;  
uint32_t RmtRec = 0;
uint8_t  RmtCnt = 0;

void TIM1_IRQHandler(void)
{
    if(TIM_GetIntStatus(TIM1,TIM_INT_CC2) != SET) 
    {
        if(RmtSta & 0x80)
        {
            RmtSta &= ~0x10;
            if((RmtSta & 0x0f) == 0x00) RmtSta |= 1<<6;

            if((RmtSta & 0x0f) < 14) RmtSta++; 
            else
            {
                RmtSta &= ~(1<<7); 
                RmtSta &= 0xf0;

            }
        }
    }

    if(TIM_GetITStatus(TIM2,TIM_IT_CC2) != RESET)    //??????
    {
        if(RDATA)
        {
            TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Falling); //?????????
            TIM_SetCounter(TIM2,0);    // ?????
            RmtSta |= 0x10;
        }
        else
        {
            Dval = TIM_GetCapture2(TIM2);  // ?????,?????????????
            TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Rising);  // ???????
            if(RmtSta & 0x10)
            {
                if(RmtSta & 0x80)
                {
                    if((Dval > 30) && (Dval < 80))
                    {
                        RmtRec <<= 1;
                        RmtRec |= 0;
                    }else if((Dval > 140) && (Dval < 180))
                    {
                        RmtRec <<= 1;
                        RmtRec |= 1;
                    }else if((Dval > 200) && (Dval < 250))
                    {
                        RmtCnt++;    // ???
                        RmtSta &= 0xf0;
                    }
                }else if((Dval > 420) && (Dval < 470))
                {
                    RmtSta |= 1<<7; //????????
                    RmtCnt = 0;    //??????
                }
            }
            RmtSta &= ~0x10;
        }
    }
    TIM_ClearFlag(TIM2,TIM_IT_Update|TIM_IT_CC2);
}

uint16_t Remote_Scan(void)
{
    uint16_t keybuf = 0xffff;
    uint8_t sta = 0xff;
    uint8_t t1,t2;

    if(RmtSta & (1<<6))//????????????
    {
        t1 = RmtRec >> 24;            //?????
        t2 = (RmtRec >> 16) & 0xff; //??????
        //printf("rmtrec = %#x\n",RmtRec);

        if((t1 == (uint8_t)~t2) && (t1 == REMOTE_ID))//???????(ID)???
        //if(t1 == (uint8_t)~t2)//???????(ID)???
        {
            //printf("t1 = %d\n",t1);
            t1 = RmtRec >> 8;
            t2 = RmtRec;
            if(t1 == (uint8_t)~t2) sta = t1;//????
        }

        if((sta != 0xff))//??????
        {
            if(RmtCnt < 10) // short key
            {
                if((RmtSta & 0x80) == 0) // release
                {
                    keybuf = RmtCnt;
                    keybuf <<= 8;
                    keybuf |= sta;

                    RmtSta &= ~(1<<6); //???????????
                    RmtCnt = 0;       //?????????
                }
            }
            #if 0
            else if(RmtCnt == 10)// && RmtCnt < 15)  // long key
            {
                keybuf = RmtCnt;
                keybuf <<= 8;
                keybuf |= sta;
            }
            else if(RmtCnt > 10)
            {
                if(((RmtCnt - 10) % 2) == 0)   //?5?????????? 
                {
                    keybuf = RmtCnt;
                    keybuf <<= 8;
                    keybuf |= sta;
                }
            }
            #else
            else if((sta == KEY_NEXT) || (sta == KEY_PREV) || (sta == KEY_MENU) || (sta == KEY_SELECT)) //?????????????
            {
                if(((RmtCnt - 10) % 2) == 0)   //?2??????????
                {
                    keybuf = RmtCnt;
                    keybuf <<= 8;
                    keybuf |= sta;
                }

                if((RmtSta & 0x80) == 0) // release
                {
                    RmtSta &= ~(1<<6); //???????????
                    RmtCnt = 0;       //?????????
                }
            }
            else //???
            {
                //if((RmtSta & 0x80) == 0)  //release ,???????????
                {
                    keybuf = RmtCnt;
                    keybuf <<= 8;
                    keybuf |= sta;

                    RmtSta &= ~(1<<6); //???????????
                    RmtCnt = 0;       //?????????

                    RmtRec = 0;       //?????
                }
            }
            #endif
        }
    }
    return keybuf;
}
